package cn.biplam.front.component.data.tree.treebase;

import java.util.ArrayList;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.tree.LinkListNode;
import cn.biplam.common.wrapper.tree.TreeNode;
import cn.biplam.front.component.data.tree.TreeCustomData;
import cn.biplam.front.component.data.tree.TreeData;
import cn.biplam.front.component.data.tree.treebase.Tree_Type.Tree_DC_Type;
import cn.biplam.front.html.base.HtmlWindow;

/**
 * 自定义树的DC
 *   <li>返回树所需要的数据TreeData
 * @author WangSong 2009-9-30上午09:10:32
 * @version 1.0
 */
public class TreeDcCustom  extends TreeDC {

	/**虚根节点ID*/
	public static final String Root_ID="-1";
	/** 展开的节点id*/
	private String nodeId;
	/** 节点向下展开几级*/
	private String levelDown;
	/** 用于从operator取自定义数据,数据返回ArrayList<TreeNode>格式*/
	private String operatorId;
	/** 取树数据的operatorId用到的参数值*/
	private String rowid;
	
	private boolean virtualNodeVisiable=false;

	/** 树数据源(列表) */
	private ArrayList<TreeNode> listData;
	/** 树数据源(链表) */
	private LinkListNode<TreeNode> linkData;
	
	/** 数据源是否是列表形式的数据源
	 *  true:列表形式数据源
	 *  false:链表形式数据源
	 *  */
	private boolean isDataListModel = true;
	
	/**
	 * 构造函数(用于树数据源为外部传入)
	 * @param mUser 用户信息
	 * @param VirtualRootName 虚根名称
	 */
	public TreeDcCustom(UserInfo mUser,
			String VirtualRootName){
		this(mUser,VirtualRootName,null,null,"1");
	}
	
	/**
	 * 构造函数
	 * @param mUser 用户信息
	 * @param VirtualRootName 虚根名称
	 * @param operatorId 用于从operator取自定义数据,数据返回ArrayList<TreeNode>格式(可以为空)
	 * @param rowid 取树数据的operatorId用到的参数值,DataFilter的默认参数,如果多个值,请用">"分割(可以为空)
	 */
	public TreeDcCustom(UserInfo mUser,
			String VirtualRootName,
			String operatorId,
			String rowid) {
		this(mUser,VirtualRootName,operatorId,rowid,"1");
	}

	/**
	 * 构造函数
	 * @param mUser 用户信息
	 * @param VirtualRootName 虚根名称
	 * @param operatorId 用于从operator取自定义数据,数据返回ArrayList<TreeNode>格式(可以为空)
	 * @param rowid 取树数据的operatorId用到的参数值,DataFilter的默认参数,如果多个值,请用">"分割
	 * @param levelDown 向下展开几级
	 */
	public TreeDcCustom(UserInfo mUser,
			String VirtualRootName,
			String operatorId,
			String rowid,
			String levelDown) {
		this(mUser,VirtualRootName,operatorId,rowid,Root_ID,levelDown);
	}

	/**
	 * 构造函数
	 * @param mUser 用户信息
	 * @param VirtualRootName 虚根名称
	 * @param operatorId 用于从operator取自定义数据,数据返回ArrayList<TreeNode>格式(可以为空)
	 * @param rowid 取树数据的operatorId用到的参数值,DataFilter的默认参数,如果多个值,请用">"分割(可以为空)
	 * @param nodeId 展开的节点id
	 * @param levelDown 向下展开几级
	 */
	public TreeDcCustom(UserInfo mUser,
			String VirtualRootName,
			String operatorId,
			String rowid,
			String nodeId,
			String levelDown) {
		this.mUser = mUser;
		this.VirtualRootName = VirtualRootName;
		this.nodeId = nodeId;
		this.levelDown = levelDown;
		this.operatorId = operatorId;
		this.rowid = rowid;
	}

	/**
	 * 
	 * @param nodeId
	 */
	public void setNodeId(String nodeId){
		this.nodeId = nodeId;
	}
	
	/**
	 * 
	 * @param levelDown
	 */
	public void setSetLevelDown(String levelDown){
		this.levelDown = levelDown;
	}
	
	/**
	 * 
	 * @param rowId
	 */
	public void setRowId(String rowId){
		this.rowid = rowId;
	}
	
	/**
	 * 
	 * @param operatorId
	 */
	public void setOperatorId(String operatorId){
		this.operatorId = operatorId;
	}
	
	/**
	 *  设置虚根节点是否显示
	 * @param virtualNodeVisialble
	 */
	public void setVirtualNodeVisiable(boolean virtualNodeVisialble){
		this.virtualNodeVisiable = virtualNodeVisialble;
	}

	@Override
	public TreeData get() throws ErrorMessage {
		TreeCustomData tcd = new TreeCustomData(nodeId,levelDown);
		if(this.isDataListModel){
			TreeNode head = new TreeNode();
			head.setNodeID(Root_ID);
			tcd.putHead(head);
			/**生成数据*/
			tcd.putData(this.getTreeData());
		}else{
			tcd.setData(this.linkData);
		}
		tcd.setVirtualRootNode(VirtualRootName,"");
		tcd.setVirtualNodeVisiable(this.virtualNodeVisiable);
		tcd.setLevelDown(TOOLS.stringToInt(levelDown));
		tcd.create();
		return tcd;
	}

	/**
	 *  设置自定义树数据源
	 * @param data 列表形式数据源<BR>
	 *             数据源中不需包含节点ID为—1的根节点
	 * @throws ErrorMessage 
	 */
	public void setData(ArrayList<TreeNode> data) throws ErrorMessage{
		if(data==null){
			throw new ErrorMessage("ArrayList<TreeNode> data is null!");
		}
		this.listData = data;
		this.isDataListModel = true;
	}

	/**
	 *  设置自定义树数据源
	 * @param data 链表形式数据源<BR>
	 *             链表中建议包含节点ID为—1的链表头
	 * @throws ErrorMessage 
	 */
	public void setData(LinkListNode<TreeNode> data) throws ErrorMessage{
		if(data==null){
			throw new ErrorMessage("LinkListNode<TreeNode> is null!");
		}
		this.linkData = data;
		this.isDataListModel = false;
	}

	@Override
	public String getTableName() throws ErrorMessage {
		return null;
	}

	@Override
	public Tree_DC_Type getType() throws ErrorMessage {
		return Tree_DC_Type.TreeCustomer;
	}

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		return null;
	}

	@Override
	public String getOperatorId() throws ErrorMessage {
		return operatorId;
	}

	@Override
	public String getRowId() throws ErrorMessage {
		return rowid;
	}

	@Override
	public String getField() throws ErrorMessage {
		return null;
	}

	/**
	 * 根据operatorId取自定义树的数据
	 *   如果返回的OperatorResult为空，抛出异常
	 * @return ArrayList<TreeNode> 自定义树的数据
	 * @throws ErrorMessage
	 */
	@SuppressWarnings("unchecked")
	private ArrayList<TreeNode> getTreeData() throws ErrorMessage{
		
		if(this.listData!=null)
			return this.listData;//列表数据由外部传入,不去请求Operator
		
		DataFilter filter = new DataFilter(rowid);
		OperatorResult or = Operator.get(operatorId, mUser.getUserID(), filter);
		if(or!=null){
			return (ArrayList<TreeNode>)or.getParser();
		}else{
			throw new ErrorMessage("TreeDcCustom getTreeData() error,TreeData is null");
		}
	}


}
